package com.huikeportal.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.controller.common.Tools;
import com.huikeportal.dao.AspnetUsersMapper;
import com.huikeportal.dao.CommonMapper;
import com.huikeportal.dao.SelectSiteMapper;
import com.huikeportal.model.AspnetUsers;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.SiteIndex;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.CommonService;
import com.huikeportal.service.FootfallGlobalService;

@Service("commonService")
public class CommonServiceImpl implements CommonService {

	@Autowired
	private CommonMapper commonMapper;

	@Autowired
	private SelectSiteMapper selectSiteMapper;

	@Autowired
	private FootfallGlobalService footfallGlobalService;
	
	@Autowired
	private AspnetUsersMapper aspnetUsersMapper;

	// 动态获取指标
	public List<String> getIndexList(List<String> userIndex, String moduleId,
			String siteType, String dateType) throws Exception {
		String usIndex = "";
		for (int i = 0; i < userIndex.size(); i++) {
			usIndex += userIndex.get(i) + ",";
		}
		usIndex = usIndex.substring(0, usIndex.length() - 1);
		List<String> resultIndex = new ArrayList<String>();
		// 根据模块id和场所类型获取对应指标信息
		SqlParameter sql = new SqlParameter();
		sql.setString(siteType);
		sql.setStringChar(moduleId);
		String menuIndex = commonMapper.getMenuIndex(sql);
		// 根据日期类型获取对应指标信息
		String dateIndex = commonMapper.getDateIndex(dateType);
		// 做交集
		String[] menuArr = menuIndex.split(",");
		String[] dateArr = dateIndex.split(",");
		String[] userArr = usIndex.split(",");
		// 获取最小权限
		int minNum = userArr.length;
		String[] minList = userArr;
		if (minNum > menuArr.length) {
			minNum = menuArr.length;
			minList = menuArr;
		}
		if (minNum > dateArr.length) {
			minNum = dateArr.length;
			minList = dateArr;
		}
		// 循环最小指标权限的指标
		for (int i = 0; i < minList.length; i++) {
			boolean flag_x = false;
			boolean flag_y = false;
			boolean flag_z = false;
			String index = minList[i];
			for (int x = 0; x < userArr.length; x++) {
				if (index.equals(userArr[x])) {
					flag_x = true;
					break;
				}
			}
			for (int y = 0; y < menuArr.length; y++) {
				if (index.equals(menuArr[y])) {
					flag_y = true;
					break;
				}
			}
			for (int z = 0; z < dateArr.length; z++) {
				if (index.equals(dateArr[z])) {
					flag_z = true;
					break;
				}
			}
			// 取交集
			if (flag_x && flag_y && flag_z) {
				resultIndex.add(index);
			}
		}
		return resultIndex;
	}

	public List<String> getIndexListOfModel(List<String> userIndex,
			String moduleId, String siteType) throws Exception {
		// 根据模块id和场所类型获取对应指标信息
		List<String> resultIndex = new ArrayList<String>();
		SqlParameter sql = new SqlParameter();
		sql.setString(siteType);
		sql.setStringChar(moduleId);
		String menuIndex = commonMapper.getMenuIndex(sql);
		List<String> menuIndexList = new ArrayList<String>();
		if (menuIndex != null && !"".equals(menuIndex)) {
			String[] menuArr = menuIndex.split(",");
			menuIndexList = Arrays.asList(menuArr);
			resultIndex = footfallGlobalService.getListJiaoji(menuIndexList,
					userIndex);
		}
		return resultIndex;
	}

	public List<String> getIndexListOfDate(String dateType) throws Exception {
		// 根据日期类型获取对应指标信息
		String dateIndex = "";
		List<String> dateIndexList = new ArrayList<String>();
		if (dateType != null && !"".equals(dateType)) {
			dateIndex = commonMapper.getDateIndex(dateType);
			if (dateIndex != null && !"".equals(dateIndex)) {
				String[] dateArr = dateIndex.split(",");
				dateIndexList = Arrays.asList(dateArr);
			}
		} else {
			// 日期为空
			return null;
		}
		return dateIndexList;
	}

	// 判断项目是否拥有指标
	public boolean isGcHaveIndex(String gcSiteId, String qySiteId,
			String lcSiteId, String siteType, String indexType)
			throws Exception {
		String gcId = "";
		// 如果传入的广场id不为空
		if (gcSiteId != null && !"".equals(gcSiteId)) {
			gcId = gcSiteId;
		}
		// 如果传入的区域id不为空
		else if (qySiteId != null && !"".equals(qySiteId)) {
			// 根据区域id查询其上级广场id
			SiteCommon site = selectSiteMapper.querySiteInfo(qySiteId);
			gcId = site.getParent300();
		}
		// 如果传入的楼层id不为空
		else if (lcSiteId != null && !"".equals(lcSiteId)) {
			// 根据楼层id查询其上级广场id
			SiteCommon site = selectSiteMapper.querySiteInfo(lcSiteId);
			gcId = site.getParent300();
		}
		// 如果广场id为空，返回false
		if ("".equals(gcId)) {
			return false;
		}
		// 根据要判断的场所类型
		SiteIndex siteIndex = new SiteIndex();
		// 如果是判断区域
		if ("0".equals(siteType)) {
			// 获取该广场下所有区域
			List<SiteAllInfo> qyList = selectSiteMapper.queryQyByGc(gcId);
			String qyString = Tools.pinjieId(qyList);
			siteIndex = selectSiteMapper.queryIsHaveIndex(qyString);
		}
		// 如果是判断楼层
		else if ("1".equals(siteType)) {
			// 获取该广场下所有楼层
			List<SiteAllInfo> lcList = selectSiteMapper.queryLcByGc(gcId);
			String lcString = Tools.pinjieId(lcList);
			siteIndex = selectSiteMapper.queryIsHaveIndex(lcString);
		}
		// 如果查询结果不为空
		if (siteIndex != null) {
			// 如果是判断视频
			if ("0".equals(indexType)) {
				if (!"0".equals(siteIndex.getIndex0())) {
					return true;
				}
			}
			// 如果是判断wifi探针
			else if ("1".equals(indexType)) {
				if (!"0".equals(siteIndex.getIndex41())) {
					return true;
				}
			}
		}
		return false;
	}
	
	public AspnetUsers getaspnetUsersMapper(UserInfo userInfo){
		AspnetUsers aspnetUsers = aspnetUsersMapper.selectByPrimaryKey(userInfo.getUserId());
		return aspnetUsers;
		
	}
	
	public boolean isDateStr(String date){
		String str = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-" +
				"(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-" +
				"(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
		Pattern  pattern = Pattern.compile(str);
		Matcher  matcher = pattern.matcher(date);
		if(matcher.matches()){
			return true;
		}else{
			return false;
		}
	}
	
	public  String getSundayOfWeek(int n){
		//n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.add(Calendar.DATE, n*7);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	
	public  String getMondayOfWeek(int n){
		//n为推迟的周数，1本周，0是上周，-1上上周，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.add(Calendar.DATE, n*7);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	
	
}
